package com.ldzl.controller.setup;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ldzl.dto.FrozenStateDTO;
import com.ldzl.pojo.BasicUnits;
import com.ldzl.pojo.CkStorageArea;
import com.ldzl.pojo.CkStorageLocation;
import com.ldzl.pojo.CkWarehouse;
import com.ldzl.service.BasicUnitsService;
import com.ldzl.service.CkStorageAreaService;
import com.ldzl.service.CkStorageLocationService;
import com.ldzl.service.CkWarehouseService;
import com.ruoyi.common.core.domain.UR;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.web.page.TableDataInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * 仓库设置控制器
 */
@RestController
@RequestMapping("/setup")
public class WarehouseController extends BaseController {

    @Autowired
    private CkWarehouseService ws;     //仓库设置

    @Autowired
    private CkStorageLocationService sls; //库区设置

    @Autowired
    private CkStorageAreaService sas; //库位设置

    @Autowired
    private BasicUnitsService us; //单位设置

    /**
     * 查询所有仓库信息
     * @return
     */
    @PostMapping("/findAll_ws")
    public TableDataInfo findAll_ws(CkWarehouse  warehouse){
        startPage();
        return getDataTable(ws.findWarehouse(warehouse));
    }

    /**
     * 获取仓库编码
     * @param code
     * @return
     */
    @GetMapping("/getNumber/{code}")
    public AjaxResult getNumber_ws(@PathVariable("code") Long code){
        return ws.getNumber(code);
    }

    /**
     * 查询指定仓库下的所有库区
     * @return
     */
    @PostMapping("/findAll_sls")
    public TableDataInfo findAll_sls(CkStorageLocation location){
        System.out.println("测试获取的库区信息："+location);

        startPage();
        return getDataTable(sls.findLocation(location));
    }


    /**
     * 查询指定库区下的所有库位
     * @return
     */
    @PostMapping("/findAll_sas")
    public TableDataInfo findAll_sas(CkStorageArea area){
        startPage();
        return getDataTable(sas.findArea(area));
    }


    /**
     * 新增/修改 仓库
     * @param warehouse
     * @return
     */
    @PostMapping("/save_ws")
    public UR save_ws(@RequestBody CkWarehouse warehouse){
        int isSuccess = ws.saveWarehouse(warehouse);
        if(isSuccess == 1)
    	    return UR.ok("新增/修改：成功");
        else if(isSuccess == 3)
            return UR.fail("仓库名称已存在");
        else
            return UR.fail("新增/修改：失败");
    }

    /**
     * 新增/修改 库区
     * @param location
     * @return
     */
    @PostMapping("/save_sls")
    public UR save_sls(@RequestBody CkStorageLocation location){
        int isSuccess = sls.saveLocation(location);

        if(isSuccess == 1)
            return UR.ok("新增/修改：成功");
        else if (isSuccess == 3)
            return UR.fail("库区名称已存在");
        else
            return UR.fail("新增/修改：失败");
    }

    /**
     * 新增/修改 库位
     * @param area
     * @return
     */
    @PostMapping("/save_sas")
    public UR save_sas(@RequestBody CkStorageArea area){
        int isSuccess = sas.saveArea(area);
        //新增/修改
        if(isSuccess == 1)
            return UR.ok("新增/修改：成功");
        else if(isSuccess == 2)
            return UR.fail("新增/修改：失败");
        else if (isSuccess == 3)
            return UR.fail("库位名称已存在");
        else if (isSuccess == 4)
            return UR.fail("位置重复");
        else if (isSuccess == 5)
            return UR.fail("库位名称和位置重复");
        else
            return UR.fail("未知错误");
    }

    /**
     * 查询所有单位
     * @return
     */
    @PostMapping("/findUnit")
    public UR findUnit(){
    	QueryWrapper<BasicUnits> queryWrapper = new QueryWrapper<>();
    	queryWrapper.eq("is_delete", "0");
    	List<BasicUnits> units = us.list(queryWrapper);
    	return UR.ok("查询成功", (List)units);
    }


    /**
     * 修改仓库冻结状态
     * @return
     */
    @PostMapping("/updateFrozen")
    public UR updateFrozen(FrozenStateDTO fst){
        if(ws.updateFrozen(fst))
            return UR.ok("修改成功");
        else
            return UR.fail("修改失败");
    }

    /**
     * 查询仓库/库区/库位
     * @return
     */
    @PostMapping("/selectWarehouse_w_l_a")
    public UR selectWarehouse_w_l_a(){
    	List<CkWarehouse> warehouse = ws.selectWarehouse_w_l_a();
    	return UR.ok("查询成功",(List)warehouse);
    }


    /**
     * 删除仓库
     * @param warehouse_id
     * @return
     */
    @GetMapping ("/deleteWarehouse/{warehouse_id}")
    public UR deleteWarehouse(@PathVariable("warehouse_id") Long warehouse_id){
        int isSuccess = ws.deleteWarehouse(warehouse_id);
        if(isSuccess == 0)
            return UR.ok("删除成功");
        else if(isSuccess == 3)
            return UR.fail("入库单存在引用，无法删除");
        else
            return UR.fail("删除失败");
    }


    /**
     * 删除库区
     * @param location_id
     * @return
     */
    @GetMapping ("/deleteLocation/{location_id}")
    public UR deleteLocation(@PathVariable("location_id") Long location_id){
    	if(sls.deleteLocation(location_id))
    		return UR.ok("删除成功");
    	else
    		return UR.fail("删除失败");
    }

    /**
     * 批量删除仓库
     * @param ids
     * @return
     */
    @PostMapping("/deleteWarehouseBatch")
    public UR deleteWarehouseBatch(@RequestBody List<Long> ids){
        String isSuccess = ws.deleteWarehouse_batch(ids);
        if(isSuccess.length() > 32)
            return UR.fail(isSuccess);
        else if (isSuccess.equals("fail"))
            return UR.fail("删除失败");
        else
            return UR.ok("删除成功");
    }

    /**
     * 批量删除库区
     * @param ids
     * @return
     */
    @PostMapping("/deleteLocationBatch")
    public UR deleteLocation_batch(@RequestBody List<Long> ids){
    	String isSuccess = sls.deleteLocation_batch(ids);
    	if(isSuccess.length() > 19)
    		return UR.fail(isSuccess);
    	else if(isSuccess.equals("fail"))
    		return UR.fail("删除失败");
    	else
    		return UR.ok("删除成功");
    }


    /**
     * 批量删除库位
     * @param ids
     * @return
     */
    @PostMapping("/deleteAreaBatch")
    public UR deleteArea_batch(@RequestBody List<Long> ids){
    	String isSuccess = sas.deleteArea_batch(ids);
    	if(isSuccess.equals("success"))
    		return UR.ok("删除成功");
    	else if(isSuccess.equals("fail"))
    		return UR.fail("删除失败");
    	else
    		return UR.fail("未知错误");
    }


    /**
     * 删除库位
     * @param area_id
     * @return
     */
    @GetMapping ("/deleteArea/{area_id}")
    public UR deleteArea(@PathVariable("area_id") Long area_id){
    	if(sas.deleteArea(area_id))
    		return UR.ok("删除成功");
    	else
    		return UR.fail("删除失败");
    }
}
